package com.woniuxy.controller;


import com.woniuxy.demo.Result;
import com.woniuxy.dto.PageShopDto;
import com.woniuxy.dto.SaveShopDto;
import com.woniuxy.enums.CodeEnum;
import com.woniuxy.exception.PawnShopException;
import com.woniuxy.param.*;
import com.woniuxy.service.ShopTableService;
import com.woniuxy.util.ValidatedUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zcx, ftl, gr, rhs
 * @since 2021-12-31
 */
@RestController
@RequestMapping("/shop-table")
@Api(tags = "门店接口")
public class ShopTableController {
    @Resource
    private ShopTableService shopTableService;

    /**
     * 查询所有门店
     */
    @ApiOperation("分页查询所有门店")
    @PostMapping("/listShop")
    public Result listShop(@RequestBody ListShopParam listShopParam) {
        PageShopDto pageShopDto = shopTableService.listShop(listShopParam);
        return Result.newInstance().setCode(200).setMsg("查询成功").setData(pageShopDto);
    }


    /**
     * 添加门店
     *
     * @param saveShopParam
     */
    @ApiOperation("添加门店")
    @PostMapping("/saveShop")
    public Result saveShop(@RequestBody @Validated SaveShopParam saveShopParam, BindingResult result) {
        //参数验证
        ValidatedUtil.ValidatedException(result);
        SaveShopDto saveShopDto = shopTableService.saveShop(saveShopParam);
        if (null != saveShopDto) {
            return Result.newInstance().setCode(200).setMsg("添加成功").setData(saveShopDto);
        }
        throw new PawnShopException(CodeEnum.SHOP_SAVE_FAIL.getMessage(), CodeEnum.SHOP_SAVE_FAIL.getCode());
    }

    /**
     * 修改门店
     *
     * @param updateShopParam
     * @return
     */
    @PostMapping("/updateShop")
    @ApiOperation("修改门店")
    public Result updateShop(@RequestBody @Validated UpdateShopParam updateShopParam, BindingResult result) {
        //参数验证
        ValidatedUtil.ValidatedException(result);
        Boolean b = shopTableService.updateShop(updateShopParam);
        if (b) {
            return Result.newInstance().setCode(200).setMsg("修改成功");
        }
        throw new PawnShopException(CodeEnum.SHOP_UPDATE_FAIL.getMessage(), CodeEnum.SHOP_UPDATE_FAIL.getCode());
    }

    /**
     * 删除门店
     *
     * @param shopIds
     * @return
     */
    @PostMapping("/delShop")
    @ApiOperation("删除门店")
    public Result delShop(@RequestBody List<Integer> shopIds) {
        if (shopTableService.delShop(shopIds)) {
            return Result.newInstance().setCode(200).setMsg("删除成功");
        }
        throw new PawnShopException(CodeEnum.SHOP_DELETE_FAIL.getMessage(), CodeEnum.SHOP_DELETE_FAIL.getCode());
    }

    /**
     * 根据Id查门店
     *
     * @param ShopId
     * @return
     */
    @GetMapping("/getShop/{ShopId}")
    @ApiOperation("根据Id查门店")
    public Result getShop(@PathVariable Integer ShopId) {
        if (!StringUtils.isEmpty(shopTableService.getShopById(ShopId))) {
            return Result.newInstance().setCode(200).setMsg("查询成功").setData(shopTableService.getShopById(ShopId));
        }
        throw new PawnShopException(CodeEnum.SHOP_SELECT_FAIL.getMessage(), CodeEnum.SHOP_SELECT_FAIL.getCode());
    }


    /**
     * 查询所有门店信息
     */
    @GetMapping("/getAllShop")
    @ApiOperation("根据Id查门店")
    public Result getAllShop() {
        return Result.newInstance().setCode(200).setMsg("查询成功")
                .setData(shopTableService.list());
    }

}

